package com.atguigu.gmall.gmalllogger.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/3/13 10:14
 */
@RestController
@Slf4j
public class LoggerController {
    
    // http://192.168.20.228:8081/applog
    @GetMapping("/applog")
    public String log(@RequestParam("param") String logString){
        // 1. 把数据落盘
        saveToDisk(logString);
        
        // 2. 把数据写入到Kafka中
        sendToKafka(logString);
        
        return "ok";
    }
    
    @Autowired
    KafkaTemplate<String, String> kafka;
    private void sendToKafka(String logString) {
        kafka.send("ods_log", logString);
    }
    
    private void saveToDisk(String logString) {
        log.info(logString);
    }
}
